<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts"/>
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true" >
          <!--          传入插槽内容 会循环多次 有多少节点循环多次-->
          <!--        作用域插槽 slot-scope='obj' 接收传递给插槽的数据 data 每个节点的数据对象-->
          <tree-tools slot-scope="{ data }" :tree-node="data" @editDepts="editDepts" @delDepts="getDepartments"  @addDepts="addDepts"/>
        </el-tree>
      </el-card>
    </div>
    <addDept  :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" ref="addDept"></addDept>
  </div>
</template>

<script>
  import TreeTools from './components/tree-tools'
  import { getDepartments} from '@/api/departments'
  import { tranListToTreeData} from '@/utils'
  import addDept from './components/add-dept'
  export default {
    data() {
      return {
        defaultProps: {
          label: 'name'
        },
        // 头部的数据结构
        company: {},
        departs: [],
        // 显示窗体
        showDialog: false,
        node:null
      }
    },
    components:{
      TreeTools,
      addDept
    },
    created() {
      this.getDepartments()
    },
    methods:{
      async getDepartments(){
        const result = await getDepartments()
        this.company = { name: result.companyName, manager: '负责人',id:''}
        this.departs = tranListToTreeData(result.depts, '')
      },
      addDepts(node){
        this.showDialog = true //显示弹层
        this.node = node // 赋值操作的节点
      },
      editDepts(node){
        this.showDialog = true // 显示弹层
        this.node = node    // 存储传递过来的node数据
        // 我们需要从这个位置 调用子组件的方法
        // 父组件 调用子组件的方法
        this.$refs.addDept.getDepartDetail(node.id)
      }
    }
  }
</script>
<style scoped>
  .tree-card {
    padding: 30px 140px;
    font-size: 14px;
  }
</style>
